<?php

declare(strict_types=1);

namespace App\ServicesNew\Impl;

use App\BO\ConvertibleCard\Helper as ConvertibleCardHelper;
use App\Entities\ConvertibleCardEntity;
use App\Entities\ConvertibleCardModel;
use App\Entities\ConvertibleCardUseRecordEntity;
use App\Entities\ConvertibleCardUseRecordModel;
use App\Entities\UserConvertibleCardEntity;
use App\Entities\UserConvertibleCardModel;
use App\ServicesNew\ConvertibleCardService;
use Illuminate\Http\Request;

class ConvertibleCardServiceImpl implements ConvertibleCardService
{
    public static function getConvertibleCardCount()
    {
        return ConvertibleCardModel::count();
    }

    public static function getConvertibleCardFirstId()
    {
        $firstCard = ConvertibleCardModel::first();
        return $firstCard->id;
    }

    /**
     * @return ConvertibleCardEntity[]
     */
    public static function getEntityList(): array
    {
        $entityList = [];
        $data = ConvertibleCardModel::orderby('created_at', 'DESC')->get();
        if ($data) {
            foreach ($data as $cardModel) {
                $entityList[] = new ConvertibleCardEntity($cardModel);
            }
        }
        return $entityList;
    }

    public static function getEntityFromRequest(Request $request)
    {
        $id = (int) $request->input('id');
        return ConvertibleCardEntity::find($id);
    }

    public static function getCardEntityById(int $cardId)
    {
        return ConvertibleCardEntity::find($cardId);
    }

    public static function getUserCardEntityByUserCardId(int $userCardId)
    {
        return UserConvertibleCardEntity::find($userCardId);
    }

    public static function userAvailableCardCount(int $userId)
    {
        $count = 0;
        $cardEntityList = ConvertibleCardHelper::getConvertibleCardEntityList();
        if ($cardEntityList) {
            foreach ($cardEntityList as $cardEntity) {
                $count += ConvertibleCardHelper::getUserAvailableCardCount($cardEntity, $userId);
            }
        }
        return $count;
    }

    public static function getUserCardEntityList(int $userId)
    {
        $userCardEntityList = [];
        $where = [
            'user_id' => $userId,
        ];
        $userCardData = UserConvertibleCardModel::where($where)->orderby('created_at', 'DESC')->get();
        if ($userCardData) {
            foreach ($userCardData as $userCardModel) {
                $userCardEntityList[] = new UserConvertibleCardEntity($userCardModel);
            }
            $userCardEntityList = self::sortUserCardEntityList($userCardEntityList);
        }
        return $userCardEntityList;
    }

    private static function sortUserCardEntityList($userCardEntityList)
    {
        foreach ($userCardEntityList as $key => $userCardEntity) {
            if (ConvertibleCardHelper::isCardUsedUp($userCardEntity)) {
                array_push($userCardEntityList, $userCardEntity);
                unset($userCardEntityList[$key]);
            }
        }
        $userCardEntityList = array_values($userCardEntityList);
        return $userCardEntityList;
    }

    public static function getUserCardRecordEntityList(int $userCardId)
    {
        $userCardRecordEntityList = [];
        $userCardRecordDataList = ConvertibleCardUseRecordModel::getUserCardRecordData($userCardId);
        if ($userCardRecordDataList) {
            foreach ($userCardRecordDataList as $cardRecordModel) {
                $userCardRecordEntityList[] = new ConvertibleCardUseRecordEntity($cardRecordModel);
            }
        }
        return $userCardRecordEntityList;
    }
}
